#include "seatreserve.h"
#include "ui_seatreserve.h"
#include "ui_seatselect.h"
#include "seatselect.h"
#include "reservehome.h"
#include "home.h"
#include "QMouseEvent"
#include "QDebug"
#include "QMessageBox"
#include "QShortcut"
#include "QStyleOptionButton"
seatReserve::seatReserve(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::seatReserve)
{
    ui->setupUi(this);
    this->setWindowFlags(Qt::FramelessWindowHint); // 设置窗口无边框
    QString text1=QString("%1(%2)").arg(reserveHome::campuses[0].name).arg(reserveHome::getCampusSeat(0));
    QString text2=QString("%1(%2)").arg(reserveHome::campuses[1].name).arg(reserveHome::getCampusSeat(1));
    QString text3=QString("%1(%2)").arg(reserveHome::campuses[2].name).arg(reserveHome::getCampusSeat(2));
    QString text4=QString("%1(%2)").arg(reserveHome::campuses[3].name).arg(reserveHome::getCampusSeat(3));
    ui->pushButton_3->setText(text1);
    ui->pushButton_4->setText(text2);
    ui->pushButton_5->setText(text3);
    ui->pushButton_6->setText(text4);
    emit ui->pushButton_3->click();
}

seatReserve::~seatReserve()
{
    delete ui;
}
void seatReserve::updateFloor(int indexCampus) {
    ui->label_2->setText("校区："+reserveHome::campuses[indexCampus].name);
    // 清空floorLayout中的所有元素
    QLayoutItem *item;
    while ((item = ui->floorLayout->takeAt(0)) != nullptr) {
        if (item->widget()) {
            delete item->widget();
        }
        delete item;
    }
    floor_selected=false;
    ui->label->setText("楼层：");
    // 清空areaLayout中的所有元素
    QLayoutItem *item1;
    while ((item1 = ui->areaLayout->takeAt(0)) != nullptr) {
        if (item1->widget()) {
            delete item1->widget();
        }
        delete item1;
    }
    area_selected=false;
    ui->label_4->setText("区域：");
    // 为新校区的每个楼层添加一个按钮
    for (int i = 0; i < reserveHome::campuses[indexCampus].floorNum; ++i) {
        QString text=QString("%1(%2)").arg(reserveHome::campuses[indexCampus].f[i].name).arg(reserveHome::getFloorSeat(i));
        QPushButton *button = new QPushButton(text, this);
            // 设置按钮的宽度和高度
            button->setFixedWidth(200);
            button->setFixedHeight(45);
            // 计算行列位置
            int row = i / 2;  // 每行两个按钮，所以用索引除以2得到行号
            int col = i % 2;  // 用索引对2取余得到列号

            // 添加按钮到布局中
            ui->floorLayout->addWidget(button, row, col);

            button->setStyleSheet(
                "QPushButton {"
                "    color: #FF0000;" /* 红色 */
                "    border: 2px solid #e0FFFF;" /* 边框 */
                "    border-radius: 10px;" /* 设置圆角半径为20像素 */

                "}"
                "QPushButton:hover {"
                "    color: #00FF00;" /* 绿色 */
                "    border: 2px solid #e0FFFF;" /* 边框 */
                "    border-radius: 10px;" /* 保持圆角半径为20像素 */

                "}"
                "QPushButton:pressed {"
                "    color: #FF0000;" /* 红色 */
                "    border: 2px solid #e0FFFF;" /* 边框 */
                "    border-radius: 10px;" /* 保持圆角半径为20像素 */

                "}"
            );

            // 连接按钮点击信号
            connect(button, &QPushButton::clicked, [=]() { updateArea(indexCampus, i, button); });
    }
}

void seatReserve::updateArea(int indexCampus,int indexFloor,QPushButton *button) {
    //首先更新刚选择的floor的按钮样式
    floor_selected=true;
    area_selected=false;
    ui->label_4->setText("区域：");
    for (int i = 0; i < ui->floorLayout->count(); ++i) {
            QLayoutItem *item = ui->floorLayout->itemAt(i);
            if (item) {
                // 获取布局项对应的控件
                QWidget *widget = item->widget();
                if (widget) {
                    // 检查控件是否是QPushButton的实例
                    QPushButton *button = qobject_cast<QPushButton *>(widget);
                    if (button) {
                        // 如果是QPushButton，设置样式表
                        button->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
                    }
                }
            }
        }
    button->setStyleSheet(basicStyle+"QPushButton{background-color:blue;}");
    ui->label->setText("楼层：" + reserveHome::campuses[indexCampus].f[indexFloor].name);
    reserveHome::index_floor=indexFloor;
    // 清空areaLayout中的所有元素
    QLayoutItem *item;
    while ((item = ui->areaLayout->takeAt(0)) != nullptr) {
        if (item->widget()) {
            delete item->widget();
        }
        delete item;
    }
qDebug()<<"select floor0";
    // 为选中楼层的每个区域添加一个按钮
    floor f = reserveHome::campuses[indexCampus].f[indexFloor]; // 假设默认是新校区
    for (int i = 0; i < f.areaNum; ++i) {
        QString text=QString("%1(%2)").arg(f.a[i].name).arg(reserveHome::getAreaSeat(i));
        QPushButton *button = new QPushButton(text, this);

            // 设置按钮的宽度和高度
            button->setFixedWidth(200);
            button->setFixedHeight(45);

            // 设计按钮样式
            button->setStyleSheet(
                "QPushButton {"
                "    color: #FF0000;" /* 红色 */
                "    border: 2px solid #e0FFFF;" /* 边框 */
                "    border-radius: 10px;" /* 设置圆角半径为20像素 */

                "}"
                "QPushButton:hover {"
                "    color: #00FF00;" /* 绿色 */
                "    border: 2px solid #e0FFFF;" /* 边框 */
                "    border-radius: 10px;" /* 保持圆角半径为20像素 */

                "}"
                "QPushButton:pressed {"
                "    color: #FF0000;" /* 红色 */
                "    border: 2px solid #e0FFFF;" /* 边框 */
                "    border-radius: 10px;" /* 保持圆角半径为20像素 */

                "}"
            );

            // 计算行列位置
            int row = i / 2;  // 每行两个按钮，所以用索引除以2得到行号
            int col = i % 2;  // 用索引对2取余得到列号

            connect(button, &QPushButton::clicked, [=]() { updateSeat(i,button); });
            // 添加按钮到布局中
            ui->areaLayout->addWidget(button, row, col);

    }
}
void seatReserve::updateSeat(int i,QPushButton *button){
    area_selected=true;
    //更新之前选择的area
    for (int i = 0; i < ui->areaLayout->count(); ++i) {
            QLayoutItem *item = ui->areaLayout->itemAt(i);
            if (item) {
                // 获取布局项对应的控件
                QWidget *widget = item->widget();
                if (widget) {
                    // 检查控件是否是QPushButton的实例
                    QPushButton *button = qobject_cast<QPushButton *>(widget);
                    if (button) {
                        // 如果是QPushButton，设置样式表
                        button->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
                    }
                }
            }
        }
    button->setStyleSheet(basicStyle+"QPushButton{background-color:blue;}");
    reserveHome::index_area=i;
    ui->label_4->setText("区域："+reserveHome::campuses[reserveHome::index_campus].f[reserveHome::index_floor].a[reserveHome::index_area].name);
}
void seatReserve::on_pushButton_7_clicked()
{
    //确认选择
    if(!campus_selected){
        QMessageBox messageBox;
        // 设置消息框的标题
        messageBox.setWindowTitle("提示");
        // 设置消息框的内容
        messageBox.setText("您还没有选择校区");
        // 设置消息框中的按钮
        messageBox.setStandardButtons(QMessageBox::Yes);
        // 设置消息框的图标
        messageBox.setIcon(QMessageBox::Warning);
        // 显示消息框，并等待用户响应
        messageBox.exec();
    }
    else if(!floor_selected){
        QMessageBox messageBox;
        // 设置消息框的标题
        messageBox.setWindowTitle("提示");
        // 设置消息框的内容
        messageBox.setText("您还没有选择楼层");
        // 设置消息框中的按钮
        messageBox.setStandardButtons(QMessageBox::Yes);
        // 设置消息框的图标
        messageBox.setIcon(QMessageBox::Warning);
        // 显示消息框，并等待用户响应
        messageBox.exec();
    }
    else if(!area_selected){
        QMessageBox messageBox;
        // 设置消息框的标题
        messageBox.setWindowTitle("提示");
        // 设置消息框的内容
        messageBox.setText("您还没有选择区域");
        // 设置消息框中的按钮
        messageBox.setStandardButtons(QMessageBox::Yes);
        // 设置消息框的图标
        messageBox.setIcon(QMessageBox::Warning);
        // 显示消息框，并等待用户响应
        messageBox.exec();
    }
    else{
    //更新座位信息
    reserveHome::importJsonToCampuses("D:\\QtCode\\Library_System_1\\library_seats.json",reserveHome::campuses);
    seatSelect*seatselect=new seatSelect;
    seatselect->show();
    this->close();
    }
}

void seatReserve::on_pushButton_clicked()
{
    reserveHome*r=new reserveHome;
    r->show();
    this->close();
}

void seatReserve::on_pushButton_3_clicked()
{
     reserveHome::index_campus=0;
     ui->pushButton_3->setStyleSheet(basicStyle+"QPushButton{background-color:blue;}");
     ui->pushButton_4->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
     ui->pushButton_5->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
     ui->pushButton_6->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
     campus_selected=true;
     updateFloor(0);
}

void seatReserve::on_pushButton_4_clicked()
{
    reserveHome::index_campus=1;
    ui->pushButton_3->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
    ui->pushButton_4->setStyleSheet(basicStyle+"QPushButton{background-color:blue;}");
    ui->pushButton_5->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
    ui->pushButton_6->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
    campus_selected=true;
    updateFloor(1);
}

void seatReserve::on_pushButton_5_clicked()
{
    reserveHome::index_campus=2;
    ui->pushButton_3->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
    ui->pushButton_4->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
    ui->pushButton_5->setStyleSheet(basicStyle+"QPushButton{background-color:blue;}");
    ui->pushButton_6->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
    campus_selected=true;
    updateFloor(2);
}

void seatReserve::on_pushButton_6_clicked()
{
    reserveHome::index_campus=3;
    ui->pushButton_3->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
    ui->pushButton_4->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
    ui->pushButton_5->setStyleSheet(basicStyle+"QPushButton{background-color:white;}");
    ui->pushButton_6->setStyleSheet(basicStyle+"QPushButton{background-color:blue;}");
    campus_selected=true;
    updateFloor(3);
}
void seatReserve::mousePressEvent(QMouseEvent *event) {
    if (event->button() == Qt::LeftButton) {
                initialMousePosition = event->globalPos(); // 记录鼠标按下时的全局位置
                isDragging = true; // 开始拖动
            }
    }

void seatReserve::mouseMoveEvent(QMouseEvent *event){
    if (isDragging && event->buttons() & Qt::LeftButton) {
                QPoint delta = event->globalPos() - initialMousePosition; // 计算鼠标移动的距离
                move(x() + delta.x(), y() + delta.y()); // 根据鼠标移动的距离来移动窗口
                initialMousePosition = event->globalPos(); // 更新鼠标位置
            }
    }
void seatReserve::mouseReleaseEvent(QMouseEvent *event) {
        if (event->button() == Qt::LeftButton) {
            isDragging = false; // 结束拖动
        }
    }

void seatReserve::keyPressEvent(QKeyEvent *event){
    // 获取floorLayout中的所有QPushButton控件
        QLayout *layout = ui->floorLayout;
        QList<QPushButton*> buttons;
        for (int i = 0; i < layout->count(); ++i) {
            QWidget *widget = layout->itemAt(i)->widget();
            if (QPushButton *button = qobject_cast<QPushButton*>(widget)) {
                buttons.append(button);
            }
        }


        // 获取areaLayout中的所有QPushButton控件
            QLayout *layout2 = ui->areaLayout;
            QList<QPushButton*> buttons2;
            for (int i = 0; i < layout2->count(); ++i) {
                QWidget *widget = layout2->itemAt(i)->widget();
                if (QPushButton *button = qobject_cast<QPushButton*>(widget)) {
                    buttons2.append(button);
                }
            }

        if(keyPositon==1){
            QList<QPushButton*> specificButtons;
            specificButtons << ui->pushButton_3 << ui->pushButton_4 << ui->pushButton_5 << ui->pushButton_6;
            switch (event->key()) {
                case Qt::Key_A:
                    reserveHome::index_campus = (reserveHome::index_campus - 1 + specificButtons.size()) % specificButtons.size();
                    break;
                case Qt::Key_D:
                    reserveHome::index_campus = (reserveHome::index_campus + 1) % specificButtons.size();
                    break;
                case Qt::Key_Enter:
                case Qt::Key_Return:
                    keyPositon=2;
                    QMetaObject::invokeMethod(buttons[0], "click", Qt::QueuedConnection);
                    return;
                default:
                    //seatReserve::keyPressEvent(event);
                    return;
                }
            // 模拟按钮点击事件
                QPushButton *button = specificButtons[reserveHome::index_campus];
                QMetaObject::invokeMethod(button, "click", Qt::QueuedConnection);
        }
        else if(keyPositon==2){
            QList<QPushButton*> specificButtons;
            specificButtons << ui->pushButton_3 << ui->pushButton_4 << ui->pushButton_5 << ui->pushButton_6;
        int newIndex = reserveHome::index_floor;
            switch (event->key()) {
            case Qt::Key_W:
                newIndex -= ui->floorLayout->columnCount();
                break;
            case Qt::Key_A:
                newIndex -= 1;
                break;
            case Qt::Key_S:
                newIndex += ui->floorLayout->columnCount();
                break;
            case Qt::Key_D:
                newIndex += 1;
                break;
            case Qt::Key_Enter:
            case Qt::Key_Return:
                keyPositon=3;
                QMetaObject::invokeMethod(buttons2[0], "click", Qt::QueuedConnection);
                return;
            case Qt::Key_Escape:
                keyPositon=1;
                //刷新floor
                QMetaObject::invokeMethod(specificButtons[0], "click", Qt::QueuedConnection);
                QMetaObject::invokeMethod(specificButtons[1], "click", Qt::QueuedConnection);
                QMetaObject::invokeMethod(specificButtons[reserveHome::index_campus], "click", Qt::QueuedConnection);
                return;
            default:
                //seatReserve::keyPressEvent(event);
                return;
            }

            if (newIndex >= 0 && newIndex < buttons.size()) {
                reserveHome::index_floor = newIndex;
                // 模拟按钮点击事件
                QPushButton *button = buttons[newIndex];
                QMetaObject::invokeMethod(button, "click", Qt::QueuedConnection);
            }
}
        else if(keyPositon==3){
            int newIndex = reserveHome::index_area;
                switch (event->key()) {
                case Qt::Key_W:
                    newIndex -= ui->areaLayout->columnCount();
                    break;
                case Qt::Key_A:
                    newIndex -= 1;
                    break;
                case Qt::Key_S:
                    newIndex += ui->areaLayout->columnCount();
                    break;
                case Qt::Key_D:
                    newIndex += 1;
                    break;
                case Qt::Key_Enter:
                case Qt::Key_Return:
                    emit ui->pushButton_7->click();
                    return;
                case Qt::Key_Escape:
                    keyPositon=2;
                    //刷新area
                    QMetaObject::invokeMethod(buttons[0], "click", Qt::QueuedConnection);
                    QMetaObject::invokeMethod(buttons[1], "click", Qt::QueuedConnection);
                    QMetaObject::invokeMethod(buttons[reserveHome::index_floor], "click", Qt::QueuedConnection);
                    return;
                default:
                    //seatReserve::keyPressEvent(event);
                    return;
                }

                if (newIndex >= 0 && newIndex < buttons2.size()) {
                    reserveHome::index_area = newIndex;
                    // 模拟按钮点击事件
                    QPushButton *button = buttons2[newIndex];
                    QMetaObject::invokeMethod(button, "click", Qt::QueuedConnection);
                }
        }
}
void seatReserve::on_pushButton_2_clicked()
{
    reserveHome*r=new reserveHome;
    r->show();
    this->close();
}


